﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "my_memory.h"

void* my_memcpy(void* arr1, const void* arr2, int num)
{
	assert(arr1 && arr2);
	int i = 0;
	for (i = 0; i < num; i++)
	{
		*(((char*)arr1) + i) = *(((char*)arr2) + i);
	}
	return (void*)arr1;
}

void* my_memmove(void* arr1, const void* arr2, int num)
{
	assert(arr1 && arr2);
	if (arr1 > arr2)
	{
		while (num--)
		{
			*(((char*)arr1) + num) = *(((char*)arr2) + num);
		}
	}
	else
	{
		int i = 0;
		for (i = 0; i < num; i++)
		{
			*(((char*)arr1) + i) = *(((char*)arr2) + i);
		}
	}
	return arr1;
}

void* my_memset(void* arr, int a, int num)
{
	for (int i = 0; i < num; i++)
	{
		*(((char*)arr) + i) = a;
	}
	return arr;
}

int my_memcmp(const void* arr1, const void* arr2, int num)
{
	for (int i = 0; i < num; i++)
	{
		if (*((char*)arr1) != *((char*)arr2))
		{
			return *((char*)arr1) - *((char*)arr2);
		}
		arr1 = (char*)arr1 + 1;
		arr2 = (char*)arr2 + 1;
	}
	return 0;
}